<template>
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      height="300"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
      :selectionType="'single'"
    >

    </FilterTable>
    <FilterTable
      ref="tables"
      height="auto"
      :dataSource="tableList"
      :columns="columns1"
      :onColumnsChange="columns => this.columns1 = columns"
      :isSelectColumn="false"
    >

    </FilterTable>
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <el-button @click="closeDrawer(2)" type="primary" :disabled='tableList.length<1'>确 定</el-button>
    </div>
  </div>
</template>
<script>
import {dealerList} from '@/api/omSale/OrderManagement/PickAddress/index.js'
import { dictionary } from '@/utils/dictMixin';
export default {
  data(){
    this.tables=null;
    return{
      topFilterList:this.createdFrom(),
      columns:this.createdTable(),
      columns1:this.createdTables(),
      fetchParams:{

      },
      appId:'beijing',
      fetchApi:dealerList,
      selectList:[],
      tableList:[],
    }
  },
  mounted(){
    this.tables=this.$refs.tables.$refs.pageTable;
  },
  mixins:[dictionary],
  methods:{
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请选择',
        },
      ]
    },
    createdTable(){
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '公司名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商简称',
          dataIndex: 'dealerShortname',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商类型',
          dataIndex: 'dealerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2538'),
          render: (p) => {
            return <span >{this.createDictText(p.row.dealerType, '2538')}</span>;
          },
        },
        {
          title: '经销商级别',
          dataIndex: 'shopLevel',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2540'),
          render: (p) => {
            return <span >{this.createDictText(p.row.shopLevel, '2540')}</span>;
          },
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '省份',
          dataIndex: 'provinceName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '城市',
          dataIndex: 'cityName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '县区',
          dataIndex: 'districtName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
      ]
    },
    createdTables(){
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.delFun(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
      ]
    },
    changeHandle(val){
      this.fetchParams=Object.assign({},this.fetchParams,val)
    },
    delFun(data) {
      this.tables.EXECUTE_DELETE([data]);
    },
    closeDrawer(type){
      if(type==1){
        this.$emit('close')
      }else{
        let arr=[],arrs=[],str='',strs='';
        this.tableList.map(x=>{
          arr.push(x.dealerCode)
          arrs.push(x.dealerName)
        })
        str=arr.join(',')
        strs=arrs.join(',')
        let json={
          dealerName:strs,
          dealerCode:str
        }
        this.$emit('close',json)
      }
    },
    selectChangeHandle(val){
      if(val.length>0){
        let json=val[0];
        if(this.tableList.length>0){
          let is=false;
            this.tableList.map(x=>{
              if(json.dealerCode==x.dealerCode){
                is=true;
              }
          })
          if(!is){
            this.tables.EXECUTE_INSERT(val);
          }else{
            this.$notify({
              title: '警告',
              message: '请不要重复添加同一经销商',
              type: 'warning'
            });
          }
        }else{
          this.tables.EXECUTE_INSERT(val);
        }


      }

    }
  },

}
</script>
